

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;

public class Execute implements ActionListener,Runnable {
	
	JFrame frame;Deflexion bt;
	
	public static void main(String argv[])
	{
		new Execute();
	}
	public Execute(){
		frame = new JFrame("Deflexion");
		
		Status sts = new Status();
		sts.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		
		bt = new Deflexion(sts,this);
		frame.addKeyListener((KeyListener)bt);
		
		///
		Container pane = frame.getContentPane();
		pane.setBackground(Color.WHITE);
		pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
		pane.add(bt);
		pane.add(sts);
		
		JMenuBar bar = new JMenuBar();
		JMenu men = new JMenu("File");
		JMenuItem item = new JMenuItem("Exit");
		item.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					System.exit(1);
				}	
			}
		);
		men.add(item);
		bar.add(men);
		//men = new JMenu("Options");
		//item = new JMenuItem("Connect to...");
		//item.addActionListener(this);
		//men.add(item);
		//bar.add(men);
		
		frame.setJMenuBar(bar);
		
		
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.pack();
		
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension size = kit.getScreenSize();		
		frame.setLocation(size.width/2-frame.getWidth()/2,size.height/2-frame.getHeight()/2);
		
		frame.setVisible(true);	
		//startServer();
	}
	public void actionPerformed(ActionEvent e){
		String p = (String)JOptionPane.showInputDialog(frame,"IP:","Connect to...",JOptionPane.PLAIN_MESSAGE);
		ip=p;
		stopServer();
		startClient();
	}
		
	void startClient(){
		try{
			client = new Socket(ip, 4321);
			out = new PrintWriter(client.getOutputStream(),true);
			in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			System.out.println("Connected to the server!");
		} catch (UnknownHostException e) {
			System.out.println("Unknown host");
			return;
		} catch  (IOException e) {
			System.out.println("No I/O");
			return;
		}
		bt.player=Deflexion.P2;
		JOptionPane.showMessageDialog(frame,"Connection successful!","Connected",JOptionPane.PLAIN_MESSAGE);
		System.out.println("I am player "+bt.player);
		bt.gameOver=false;
		new Thread(this).start();	
	}
	
	void startServer(){
		try{
			server = new ServerSocket(4321); 
		} catch (IOException e) {
			System.out.println("Could not listen on port");
			return;
		}
		
		try{
			System.out.println("Waiting for client...");
			client = server.accept();
		} catch (IOException e) {
			System.out.println("Accept failed");
			return;
		}
		
		try{
			in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			out = new PrintWriter(client.getOutputStream(), true);
			System.out.println("Found a client");
		} catch (IOException e) {
			System.out.println("Read failed");
			return;
		}
		bt.player=Deflexion.P1;
		JOptionPane.showMessageDialog(frame,"Connection successful!","Connected",JOptionPane.PLAIN_MESSAGE);
		System.out.println("I am player "+bt.player);
		bt.gameOver=false;
		new Thread(this).start();	
	}
	void stopServer(){
		client=null;
		server=null;
		System.out.println("Stopped listening");
	}
	
	String ip;
	ServerSocket server;
	Socket client;
	BufferedReader in;
	PrintWriter out;
	
	public void run(){
		int typ=-1,y=-1,x=-1,prevy=-1,prevx=-1,rot=-1;
		
		while(true){
			// listen for the other persons moves
			
			try {
				// 1 - swap
				// 2 - normal
				typ=Integer.parseInt(in.readLine());
			} catch (Exception ie){
				//System.out.println("Move-type error");	
			}
				
			if(typ==1){
				
				//System.out.println("Swap move");
				
				try {
					y=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("Y axis error");	
				}
				try {
					x=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("X axis error");	
				}
				try {
					prevy=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("prevy axis error");	
				}
				try {
					prevx=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("prevx axis error");	
				}
				
				System.out.println(prevy+","+prevx+" swaps with "+y+","+x);
				bt.battlefield[prevy][prevx].swapWith(y,x);		
			} else if(typ==2) {
				
				//System.out.println("Normal move");
				
				try {
					y=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("Y axis error");	
				}
				try {
					x=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("X axis error");	
				}
				try {
					prevy=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("prevy axis error");	
				}
				try {
					prevx=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("prevx axis error");	
				}
				try {
					rot=Integer.parseInt(in.readLine());
				} catch (Exception ie){
					//System.out.println("rot error");	
				}
				
				//bt.battlefield[y][x]=bt.battlefield[prevy][prevx];	
				System.out.println(prevy+","+prevx+" swaps with "+y+","+x);	
				bt.battlefield[prevy][prevx].swapWith(y,x);		
				bt.battlefield[prevy][prevx].rot=rot;
			} else {
				//System.out.println("No such move-type");	
			}
			
			//System.out.println("next turn...");
			bt.nextTurn();
		}
	}
}